package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotFtpUploadsService;
import com.lechi.web.system.domain.IotFtpUploads;
import com.lechi.web.system.mapper.iot.IotFtpUploadsMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 设备上传图片Service业务层处理
 *
 * @author yangguang
 * @date 2024 -06-03
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotFtpUploadsServiceImpl implements IIotFtpUploadsService {

    private final IotFtpUploadsMapper iotFtpUploadsMapper;

    /**
     * 查询设备上传图片
     *
     * @param id 设备上传图片主键
     * @return 设备上传图片
     */
    @Override
    public IotFtpUploads selectIotFtpUploadsById(Long id) {
        try {
            return iotFtpUploadsMapper.selectIotFtpUploadsById(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "根据ID查询上传图片记录", method, id));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询设备上传图片列表
     *
     * @param iotFtpUploads 设备上传图片
     * @return 设备上传图片
     */
    @Override
    public IotFtpUploads selectLastIotFtpUploads(IotFtpUploads iotFtpUploads) {
        try {
            return iotFtpUploadsMapper.selectLastIotFtpUploads(iotFtpUploads);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询最新上传图片记录", method, iotFtpUploads));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询设备上传图片列表
     *
     * @param iotFtpUploads 设备上传图片
     * @return 设备上传图片
     */
    @Override
    public List<IotFtpUploads> selectIotFtpUploadsList(IotFtpUploads iotFtpUploads) {
        try {
            return iotFtpUploadsMapper.selectIotFtpUploadsList(iotFtpUploads);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询设备上传图片列表", method, iotFtpUploads));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增设备上传图片
     *
     * @param iotFtpUploads 设备上传图片
     */
    @Override
    public void insertIotFtpUploads(IotFtpUploads iotFtpUploads) {
        try {
            iotFtpUploadsMapper.insertIotFtpUploads(iotFtpUploads);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "设备上传图片存储上传记录", method, iotFtpUploads));
            log.error("设备上传图片存储上传记录失败", e);
        }
    }

    /**
     * 删除设备上传图片
     *
     * @param iotFtpUploads 设备上传图片
     */
    @Override
    public void deleteIotFtpUploadsByParams(IotFtpUploads iotFtpUploads) {
        try {
            iotFtpUploadsMapper.deleteIotFtpUploadsByParams(iotFtpUploads);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删设备上传图片记录", method, iotFtpUploads));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

    /**
     * 批量删除设备上传图片
     *
     * @param ids 需要删除的设备上传图片主键
     */
    @Override
    public void deleteIotFtpUploadsByIds(Long[] ids) {
        try {
            iotFtpUploadsMapper.deleteIotFtpUploadsByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除设备上传图片记录", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }

}
